package com.boge.springframework.beans.factory.support;

import com.boge.springframework.beans.BeansException;
import com.boge.springframework.beans.factory.FactoryBean;
import com.boge.springframework.beans.factory.config.BeanDefinition;
import com.boge.springframework.beans.factory.config.BeanPostProcessor;
import com.boge.springframework.beans.factory.config.ConfigurableBeanFactory;
import com.boge.springframework.util.ClassUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建Bean的实现类
 * 采用模板设计模式：
 * 1. 查找缓存  ------缓存在DefaultSingletonBeanRegistry 中维护
 * 2. 没有缓存
 *      2.1： 获取BeanDefinition信息  --- 子类完成
 *      2.2： 反射创建Bean对象   --- 子类完成
 *
 *  扩展： 维护一个BeanPostProcessor, 提供 BeanPostProcessor的注册和获取
 */
public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {

    private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
    /** BeanPostProcessors to apply in createBean */
    private final List<BeanPostProcessor> beanPostProcessors = new ArrayList<BeanPostProcessor>();

    @Override
    public Object getBean(String name) throws BeansException {
        return doGetBean(name, null);
    }

    @Override
    public Object getBean(String name, Object... args) throws BeansException {
        return doGetBean(name, args);
    }

    @Override
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        return (T) getBean(name);
    }

    /***
     * 真正实现创建Bean的方法
     * @param name
     * @param args
     * @param <T>
     * @return
     */
    protected <T> T doGetBean(final String name, final Object[] args) {
        // 查找缓存
        Object sharedInstance = getSingleton(name);
        if (sharedInstance != null) {
            // 如果是 FactoryBean，则需要调用 FactoryBean#getObject
            return (T) getObjectForBeanInstance(sharedInstance, name);
        }
        // 获取BeanDefinition信息
        BeanDefinition beanDefinition = getBeanDefinition(name);
        // 创建Bean --反射创建
        Object bean = createBean(name, beanDefinition, args);
        return (T) getObjectForBeanInstance(bean, name);
    }

    /***
     * 判断object是FactoryBean，
     * 是： 直接从自定义中获取
     * 不是：从一级缓存中获取
     * @param beanInstance
     * @param beanName
     * @return
     */
    private Object getObjectForBeanInstance(Object beanInstance, String beanName) {
        if (!(beanInstance instanceof FactoryBean)) {
            return beanInstance;
        }

        Object object = getCachedObjectForFactoryBean(beanName);

        if (object == null) {
            FactoryBean<?> factoryBean = (FactoryBean<?>) beanInstance;
            object = getObjectFromFactoryBean(factoryBean, beanName);
        }

        return object;
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException;

    @Override
    public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor){
        this.beanPostProcessors.remove(beanPostProcessor);
        this.beanPostProcessors.add(beanPostProcessor);
    }

    /**
     * Return the list of BeanPostProcessors that will get applied
     * to beans created with this factory.
     */
    public List<BeanPostProcessor> getBeanPostProcessors() {
        return this.beanPostProcessors;
    }

    public ClassLoader getBeanClassLoader() {
        return this.beanClassLoader;
    }
}
